package com.cy.store.service.impl;

import com.cy.store.domain.Product;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.IProductService;
import com.cy.store.service.ex.ProductNoFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2025/2/27 10:00
 */
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Product> findHotProducts() {
        List<Product> products = productMapper.findHotProducts();
        /**
         * 循环遍历，将一些不需要返回给前端的属性设置为null，
         */
        for(Product p : products){
            p.setPriority(null);
            p.setCreatedUser(null);
            p.setCreatedTime(null);
            p.setModifiedUser(null);
            p.setModifiedTime(null);
        }
        return products;
    }
    @Override
    public Product getById(Integer id){
        Product product = productMapper.findById(id);
        if(product == null){
            throw new ProductNoFoundException("尝试访问的商品数据不存在");
        }
        product.setPriority(null);
        product.setCreatedUser(null);
        product.setCreatedTime(null);
        product.setModifiedTime(null);
        product.setModifiedUser(null);
        return product;
        }
}
